package com.example.his.api.front.service.impl;


import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.example.his.api.config.AliPayConfig;
import com.example.his.api.front.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author huixh
 */
@Slf4j
@Service("AliPayServiceImpl")
public class AliPayServiceImpl implements PayService {

    @Resource
    private AliPayConfig aliPayConfig;
    @Override
    public String toPay(Map<String,Object> subject, String payOrderNo, BigDecimal money) throws Exception{
        String subjectInfo = MapUtil.getStr(subject, "subject");
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
                .preCreate(subjectInfo, payOrderNo, String.valueOf(money));
        String payForm = null;
        if (ResponseChecker.success(response)) {
            payForm = response.getQrCode();
        } else {
            log.error("支付宝预下单失败! alipay");
        }
        return payForm;
    }
}
